import java.io.*;

/**
 * Created by Agrawal Bhushan on 10-04-2016.
 */
public class Quadraple_generator {

    static Parser parse = new Parser();
    static int cnt = 1;
    static boolean first_read = true;
    FileWriter fw;

    public void generateCode_arithmatic(String op) throws Exception {

        BufferedReader br = new BufferedReader(new FileReader("G:\\BE - LAST SEM\\bka\\Labs\\CD Lab\\2\\Productions.txt"));

        LineNumberReader lnr = new LineNumberReader(new FileReader(new File("G:\\BE - LAST SEM\\bka\\Labs\\CD Lab\\2\\Productions.txt")));
        lnr.skip(Long.MAX_VALUE);
        System.out.println("Number of Productions : " + (lnr.getLineNumber() + 1));
        int a = lnr.getLineNumber() + 1;
        lnr.close();

        String[] productions = new String[a];
        String sCurrentLine;
        int i = 0;
        while ((sCurrentLine = br.readLine()) != null) {
            productions[i] = sCurrentLine;
            i++;
        }

        String id1, id2;
        String prod = productions[Integer.parseInt(op) - 1];
        id2 = (String) parse.ids.pop();
        id1 = (String) parse.ids.pop();
        String temp = "t" + cnt;
        parse.ids.push(temp);

        String newToWrite = temp + " = " + id1 + " " + prod.charAt(6) + " " + id2 + "\n";
        cnt++;
        System.out.println(temp + "=" + id1 + prod.charAt(6) + id2 + "\n");
        writeToFile(newToWrite);
    }

    private void writeToFile(String arr) throws IOException {

        if (first_read) {
            first_read = false;
            fw = new FileWriter(new File("G:\\BE - LAST SEM\\bka\\Labs\\CD Lab\\2\\InterC.txt"));
            fw.write(arr);
            fw.close();
            return;
        }

        BufferedReader br = new BufferedReader(new FileReader("G:\\BE - LAST SEM\\bka\\Labs\\CD Lab\\2\\InterC.txt"));
        String temp, fileContent = "";
        while ((temp = br.readLine()) != null) {
            fileContent += temp + "\n";
        }

        fw = new FileWriter(new File("G:\\BE - LAST SEM\\bka\\Labs\\CD Lab\\2\\InterC.txt"));
        fw.write(fileContent);
        fw.append(arr);
        fw.close();

    }

    public void generateCode_assignment() throws Exception {

        BufferedReader br = new BufferedReader(new FileReader("G:\\BE - LAST SEM\\bka\\Labs\\CD Lab\\2\\Productions.txt"));
        LineNumberReader lnr = new LineNumberReader(new FileReader(new File("G:\\BE - LAST SEM\\bka\\Labs\\CD Lab\\2\\Productions.txt")));
        lnr.skip(Long.MAX_VALUE);
        System.out.println("Number of Productions : " + (lnr.getLineNumber() + 1)); //Add 1 because line index starts at 0
        int a = lnr.getLineNumber() + 1;
        lnr.close();

        String[] productions = new String[a];
        String sCurrentLine;
        int i = 0;
        while ((sCurrentLine = br.readLine()) != null) {
            productions[i] = sCurrentLine;
            i++;
        }

        String id1, id2;
        id2 = (String) parse.ids.pop();
        id1 = (String) parse.ids.pop();

        String newToWrite = id1 + " = " + id2;
        System.out.println(newToWrite);
        writeToFile(newToWrite);

    }
}
